
//相关业务逻辑
$(function(){
	page.bindTag();
	page.getMaterialMergeDate();
	page.getAllMaterial();
	page.getTreeDate();
});


var table;
var page={//页面总变量
		bindTag:function(){
//			切换视图
			$("#changeView").bind("click",function(){
				$("#otherTreeDiv").toggleClass("hidden");
				$("#treeLeftToRightDiv").toggleClass("hidden");
				echarts.getInstanceByDom(document.getElementById('treeLeftToRightDiv')).resize();
			});
			
			
			
			//页面添加按钮    显示，模态框
			$("#addPageBtn").bind("click",function(){
				 $("#addModal").modal("show");
			});
			
			//模态框添加按钮
			$("#addModalBtn").bind("click",function(){
				var data=new Object();
				data.type=$("#type option:selected").val();
				data.materialType=$("#materialType option:selected").val();
				data.isReport=$("#isReport option:selected").val();
				data.isPurchase=$("#isPurchase option:selected").val();
				data.secondIsShow=$("#secondIsShow option:selected").val();
				data.allIsReport=$("#allIsReport option:selected").val();
				data.isReadColor=$("#isReadColor option:selected").val();
				if(data.type==""){
					layAlert("请选择所属类型");
					return false;
				}
				if(data.maching==""){
					layAlert("请选择加工对象");
					return false;
				}
				ajaxUtil.ajaxRuquest("structureTree/addMaterialTreeData",data,function(data){
					$("#addModal").modal("hide");
					layer.msg(data,{icon:1});
					page.getTreeDate();
					table.ajax.reload(null, false);
				});
			});
			
			//修改
			 $("#updateModalBtn").bind("click",function(){
				 var data=new Object();
				    data.id=$("#modalId").val();
					data.isReport=$("#updateIsReport option:selected").val();
					data.isPurchase=$("#updateIsPurchase option:selected").val();
					data.secondIsShow=$("#updateSecondIsShow option:selected").val();
					data.allIsReport=$("#updateAllIsReport option:selected").val();
					data.isReadColor=$("#updateIsReadColor option:selected").val();
					ajaxUtil.ajaxRuquest("structureTree/updateMaterialTreeData",data,function(data){
						$("#updateModal").modal("hide");
						page.getTreeDate();
						layer.msg(data,{icon:1});
						table.ajax.reload(null, false);
					});
			 });
			
			
			
			//删除
			$("#treeDataTbale tbody").on("click",".delete",function(){
				var id=$(this).val();
				ajaxUtil.ajaxRuquest("structureTree/deleteMaterialTreeData",{'id':id},function(data){
					layer.msg(data,{icon:1});
					page.getTreeDate();
					table.ajax.reload(null, false);
				});
			});
			
			
			//修改
			$("#treeDataTbale tbody").on("click",".update",function(){
				var id=$(this).val();
				ajaxUtil.ajaxRuquest("structureTree/getMaterialTreeByMap",{'id':id},function(data){
					if(data.type==1){
						$("#updateType").text("整架");
					}else if(data.type==2){
						$("#updateType").text("框");
					}else if(data.type==3){
						$("#updateType").text("腿");
					}
					 $("#updateMaterialType").text(data.materialType);
					 $("#updateIsReport").find("option[value='"+data.isReport+"']").attr("selected","selected");
					 $("#updateIsPurchase").find("option[value='"+data.isPurchase+"']").attr("selected","selected");
					 $("#updateSecondIsShow").find("option[value='"+data.secondIsShow+"']").attr("selected","selected");
					 $("#updateAllIsReport").find("option[value='"+data.allIsReport+"']").attr("selected","selected");
					 $("#updateIsReadColor").find("option[value='"+data.isReadColor+"']").attr("selected","selected");
					 $("#updateModal").modal("show");
					 $("#modalId").val(data.id);
				});
			});
			
		},
		getMaterialMergeDate:function(){
			table=ajaxUtil.dataTablesAjax("treeDataTbale",15,"structureTree/getMaterialTreeData",
					{},[
					{"data":"type",render: function (data,type,row){
						if(data==1){
							return "整架";
						}
						if(data==2){
							return  "框";
						}
						if(data==3){
							return "腿";
						}
					}},
					{"data":"materialType"},
					{"data":"isReport",render: function (data,type,row){
						if(data==1){
							return "是";
						}
						if(data==2){
							return  "否";
						}
					}},
					{"data":"isPurchase",render: function (data,type,row){
						if(data==1){
							return "是";
						}
						if(data==2){
							return  "否";
						}
					}},
					{"data":"secondIsShow",render: function (data,type,row){
						if(data==1){
							return "是";
						}
						if(data==2){
							return  "否";
						}
					}},
					{"data":"allIsReport",render: function (data,type,row){
						if(data==1){
							return "是";
						}
						if(data==2){
							return  "否";
						}
					}},
					{"data":"isReadColor",render: function (data,type,row){
						if(data==1){
							return "是";
						}
						if(data==2){
							return  "否";
						}
					}},
					{"data":"id",render: function (data,type,row){
						return "<button class='btn btn-primary delete' value='"+data+"'>删除</button> &nbsp;&nbsp;" +
								"<button class='btn btn-primary update' value='"+data+"'>修改</button>";
					}}
			       ]
		        );
		},
		getAllMaterial:function(){
			ajaxUtil.ajaxRuquest("basicData/getRowMaterialType",{"isvolid":1},function(data){
				var html="";
				for(var i=0;i<data.length;i++){
					html+="<option value='"+data[i]+"'>"+data[i]+"</option>" ;
				}
				$("#materialType").append(html);
			});
		},
		getTreeDate:function(){
			ajaxUtil.ajaxRuquest("structureTree/geAlltMaterialTree",null,function(data){
				console.log(data);
				var object=new Object();
				object.name="整架";
				var array=new Array();
				for(var i=0;i<data.length;i++){
					var listTree=data[i].listTree;
					var type=new Object();
					type.name=data[i].type;
					var typeArray=new Array();
					for(var j=0;j<listTree.length;j++){
					//是否读取颜色
					var isReadColorStr=new Object();
					isReadColorStr.name=listTree[j].isReadColorStr;
						//整架是否显示
						var allIsReportStr=new Object();
						allIsReportStr.name=listTree[j].allIsReportStr;
						var allIsReportStrArray=new Array();
						allIsReportStrArray.push(isReadColorStr);
						allIsReportStr.children=allIsReportStrArray;
							//整框整腿
							var secondIsShowStr=new Object();
							secondIsShowStr.name=listTree[j].secondIsShowStr;
							var secondIsShowStrArray=new Array();
							secondIsShowStrArray.push(allIsReportStr);
							secondIsShowStr.children=secondIsShowStrArray;
								//是否采购
								var isPurchaseStr=new Object();
								isPurchaseStr.name=listTree[j].isPurchaseStr;
								var isPurchaseStrArray=new Array();
								isPurchaseStrArray.push(secondIsShowStr);
								isPurchaseStr.children=isPurchaseStrArray;
								    //是否报工
									var isReportStr=new Object();
									isReportStr.name=listTree[j].isReportStr;
									var isReportStrArray=new Array();
									isReportStrArray.push(isPurchaseStr);
									isReportStr.children=isReportStrArray;
									    //物料类型
										var materialType=new Object();
							            materialType.name=listTree[j].materialType
							            var materialTypeArray=new Array();
										materialTypeArray.push(isReportStr);
										materialType.children=materialTypeArray;
										    //类型子信息
											typeArray.push(materialType);
					}
					type.children=typeArray;
					array.push(type);
				}
				object.children=array;
				page.getTreeView(object);
				console.log(object);
			});
			
		},
		getTreeView:function(data){
			var myChart=echarts.init(document.getElementById('treeLeftToRightDiv'));
			myChart.showLoading();
			myChart.hideLoading();

//			    echarts.util.each(data.children, function (datum, index) {
//			        index % 2 === 0 && (datum.collapsed = true);
//			    });

			    myChart.setOption(option = {
			        tooltip: {
			            trigger: 'item',
			            triggerOn: 'mousemove'
			        },
			        series: [
			            {
			                type: 'tree',

			                data: [data],

			                top: '1%',
			                left: '7%',
			                bottom: '1%',
			                right: '20%',

			                symbolSize: 7,

			                label: {
			                    normal: {
			                        position: 'left',
			                        verticalAlign: 'middle',
			                        align: 'right',
			                        fontSize: 15
			                    }
			                },

			                leaves: {
			                    label: {
			                        normal: {
			                            position: 'right',
			                            verticalAlign: 'middle',
			                            align: 'left'
			                        }
			                    }
			                },

			                expandAndCollapse: true,
			                animationDuration: 550,
			                animationDurationUpdate: 750
			            }
			        ]
			});
		}
}

//let data = allRes;
////属性配置信息
//let attributes = {
//   id: 'resourcesId',
//   parentId: 'resParentId',
//   name: 'resName',
//   rootId: 1
//};
////let treeData = toTreeData(data, attributes);
//function toTreeData (data, attributes) {
//	  let resData = data;
//	  let tree = [];
//
//	  for (let i = 0; i < resData.length; i++) {
//	    if (resData[i].resParentId === attributes.rootId) {
//	      let obj = {
//	        id: resData[i][attributes.id],
//	        title: resData[i][attributes.name],
//	        children: []
//	      };
//	      tree.push(obj);
//	      resData.splice(i, 1);
//	      i--;
//	    }
//	  }
//	  run(tree);
//	  function run(chiArr) {
//	    if (resData.length !== 0) {
//	      for (let i = 0; i < chiArr.length; i++) {
//	        for (let j = 0; j < resData.length; j++) {
//	          if (chiArr[i].id === resData[j][attributes.parentId]) {
//	            let obj = {
//	              id: resData[j][attributes.id],
//	              title: resData[j][attributes.name],
//	              children: []
//	            };
//	            chiArr[i].children.push(obj);
//	            resData.splice(j, 1);
//	            j--;
//	          }
//	        }
//	        run(chiArr[i].children);
//	      }
//	    }
//	  }
//	  return tree;
//	}

